#ifndef CONVERSION_H_INCLUDED
#define CONVERSION_H_INCLUDED

/*
    XFlightRec
    Copyright (C) 2009 Mike Gehl

    X-Plane Plugin SDK
    Copyright (C) Sandy Barbour and Ben Supnik

    Data conversion routines to NAV3
    Copyright (C) 2000 Ted Wright
    NASA Business: wright@grc.nasa.gov, Remainder: wright@en.com

    This file is part of XFlightRec.

    XFlightRec is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    XFlightRec is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with XFlightRec.  If not, see <http://www.gnu.org/licenses/>.
    ---------------------------------------------------------------------------
    conversion.h
    Copyright (C) Mike Gehl 2009 <mjgehl@gmail.com>

    Convert x-plane datarefs to those that can be read by NAV.
    For a desciption of the NAV data format, see xflightrec.h.
*/

#include <inttypes.h>
#include "xflightrec.h"


/* Lattitude conversion factor (SEE NAV3 source) */
static const double LAT_CONVERSION = 45.0 / 5000875.0;

/* Longitude conversion factor (SEE NAV3 source) */
static const double LON_CONVERSION = 90.0 / (256.0 * 4194304.0);

/* Feet per meter conversion factor */
static const double FEET_PER_METER = 3.28084;

/* Degrees to meter conversion factor (SEE NAV3 source) */
static const double DEGREES_TO_METERS = 10001750.0 / 90.0;

/* meters/sec to knots conversion factor */
static const double METERS_TO_KNOTS = 1.94384449;


/**
 * Convert the numbers of seconds and populate the FLTREC_DATA structure's
 * wClockHour, wClockMin, wClockSec.
 *
 * @param seconds Zulu time (seconds since midnight??).
 * @param flt_rec Pointer to FLTREC_DATA.
 * @see fltrec.h
 */
void to_time(double, FLTREC_DATA*);


/**
 * Convert the aircraft's latitude and populate the FLTREC_DATA structure's
 * dwLatLo, dwLatHi.
 *
 * @param lat      The latitude (degrees, decimal format) of the aircraft.
 * @param flt_rec  Pointer to FLTREC_DATA.
 * @see fltrec.h
 */
void to_lattitude(double, FLTREC_DATA*);


/**
 * Convert the aircraft's longitude and populate the FLTREC_DATA structure's
 * dwLonLo, dwLonHi.
 *
 * @param lon      The longitude (degrees, decimal format) of the aircraft.
 * @param flt_rec  Pointer to FLTREC_DATA.
 * @see fltrec.h
 */
void to_longitude(double, FLTREC_DATA*);


/**
 * Convert the aircraft's altitude and populate the FLTREC_DATA structure's
 * dwAltLo, dwAltHi.
 *
 * @param alt      The elevation above MSL (in meters) of the aircraft.
 * @param flt_rec  Pointer to FLTREC_DATA.
 * @see fltrec.h
 */
void to_altitude(double, FLTREC_DATA*);


/**
 * Convert a 360 orientation to the FLTREC_DATA format
 *
 * @param  orient Degree value of x-plane orientation
 * @see sim/flightmodel/position/theta
 * @see sim/flightmodel/position/phi
 * @see sim/flightmodel/position/psi
 * @return FLTREC_DATA formatted orientaion
 */
DWORD to_orientation(double);


/**
 * Convert an airspeed in knots to the FLTREC_DATA format
 *
 * @param  speed Airspeed in knots
 * @see sim/flightmodel/position/indicated_airspeed
 * @return FLTREC_DATA formatted airspeed
 */
DWORD to_airspeed(double);

/**
 * Convert an airspeed in meters/sec to the FLTREC_DATA format
 *
 * @param  speed Airspeed in meters/sec
 * @see sim/flightmodel/position/true_airspeed
 * @return FLTREC_DATA formatted airspeed
 */
double meters_per_sec_to_knots(double);

/**
 * Convert the landing gear position to the FLTREC_DATA format
 *
 * @param  pos Gear Position, 0=up, 1=down
 * @see sim/flightmodel2/gear/deploy_ratio
 * @return FLTREC_DATA formatted gear position
 */
WORD to_gear_pos(double);

#endif // CONVERSION_H_INCLUDED
